package com.czh.www.gui;

import com.czh.www.domain.SystemVar;
import com.czh.www.util.CzhResources;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import javax.swing.BorderFactory;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;

public class MsgWindow extends JFrame {
    private String ip;
    private String content;
    private JButton jButton1;
    private JButton jButton2;
    private JLabel jLabel1;
    private JLabel jLabel2;
    private JPanel jPanel1;
    private JScrollPane jScrollPane1;
    private JTextArea jTextArea1;

    public MsgWindow() {
        this.initComponents();
    }

    public MsgWindow(String content, String senderName, String senderHost, String ip) {
        this.initComponents();
        this.jLabel1.setText(senderName + "(" + senderHost + ")");
        this.jLabel2.setText((new Date()).toString());
        this.jTextArea1.setText(content);
        this.ip = ip;
        this.content = content.trim();
    }

    private void initComponents() {
        this.jPanel1 = new JPanel();
        this.jLabel1 = new JLabel();
        this.jLabel2 = new JLabel();
        this.jScrollPane1 = new JScrollPane();
        this.jTextArea1 = new JTextArea();
        this.jButton1 = new JButton();
        this.jButton2 = new JButton();
        this.setDefaultCloseOperation(2);
        this.setIconImage(CzhResources.getImage("/ipmsg.JPG"));
        this.jPanel1.setBorder(BorderFactory.createTitledBorder("消息来自"));
        this.jLabel1.setHorizontalAlignment(0);
        this.jLabel1.setText("  ");
        this.jLabel2.setHorizontalAlignment(0);
        this.jLabel2.setText("  ");
        GroupLayout jPanel1Layout = new GroupLayout(this.jPanel1);
        this.jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(jPanel1Layout.createParallelGroup(Alignment.LEADING).addGroup(jPanel1Layout.createSequentialGroup().addGap(23, 23, 23).addGroup(jPanel1Layout.createParallelGroup(Alignment.TRAILING, false).addComponent(this.jLabel2, Alignment.LEADING, -1, -1, 32767).addComponent(this.jLabel1, Alignment.LEADING, -1, 304, 32767)).addContainerGap(32, 32767)));
        jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(Alignment.LEADING).addGroup(jPanel1Layout.createSequentialGroup().addComponent(this.jLabel1).addPreferredGap(ComponentPlacement.RELATED).addComponent(this.jLabel2).addContainerGap(-1, 32767)));
        this.jTextArea1.setColumns(20);
        this.jTextArea1.setEditable(false);
        this.jTextArea1.setRows(5);
        this.jTextArea1.setEnabled(false);
        this.jScrollPane1.setViewportView(this.jTextArea1);
        this.jButton1.setText("关闭");
        this.jButton1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                MsgWindow.this.jButton1ActionPerformed(evt);
            }
        });
        this.jButton2.setText("回复");
        this.jButton2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                MsgWindow.this.jButton2ActionPerformed(evt);
            }
        });
        GroupLayout layout = new GroupLayout(this.getContentPane());
        this.getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(Alignment.LEADING).addGroup(layout.createSequentialGroup().addGroup(layout.createParallelGroup(Alignment.LEADING).addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout.createParallelGroup(Alignment.TRAILING, false).addComponent(this.jScrollPane1, Alignment.LEADING).addComponent(this.jPanel1, Alignment.LEADING, -1, -1, 32767))).addGroup(layout.createSequentialGroup().addGap(98, 98, 98).addComponent(this.jButton1, -2, 69, -2).addGap(42, 42, 42).addComponent(this.jButton2, -2, 68, -2))).addContainerGap(14, 32767)));
        layout.setVerticalGroup(layout.createParallelGroup(Alignment.LEADING).addGroup(layout.createSequentialGroup().addContainerGap().addComponent(this.jPanel1, -2, -1, -2).addPreferredGap(ComponentPlacement.UNRELATED).addComponent(this.jScrollPane1, -2, 146, -2).addPreferredGap(ComponentPlacement.UNRELATED).addGroup(layout.createParallelGroup(Alignment.BASELINE).addComponent(this.jButton1).addComponent(this.jButton2)).addContainerGap(-1, 32767)));
        this.pack();
    }

    private void jButton1ActionPerformed(ActionEvent evt) {
        this.dispose();
    }

    private void jButton2ActionPerformed(ActionEvent evt) {
        int rowCount = SystemVar.getUserIndex(this.ip);
        String[] tempStrs = this.content.split("\n");
        StringBuffer str = new StringBuffer("");

        for(int i = 0; i < tempStrs.length; ++i) {
            str.append(">" + tempStrs[i] + "\n");
        }

        SystemVar.getUserListGui().rebackMsg(rowCount, str.substring(0, str.length() - 1));
        this.dispose();
    }

    public static void main1(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                (new MsgWindow()).setVisible(true);
            }
        });
    }
}
